<?php

namespace App\Http\Controllers;

use App\Models\Banner;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Http\Requests\Banner\UpdateBannerResource;
use App\Http\Requests\Banner\DestroyBannerResource;
use App\Http\Requests\Banner\QueryBannerCollection;

class BannerController extends Controller
{
	/**
	 * 获取轮播图资源集合
	 *
	 * Date: 2018/2/28
	 * @author George
	 * @param QueryBannerCollection $request
	 * @return \Illuminate\Http\JsonResponse
	 */
    public function index(QueryBannerCollection $request)
    {
        $banners = Banner::orderBy('number')->get();

        foreach ($banners as $banner){
            $banner->name = basename($banner->image);
            $banner->image = config('app.url').$banner->image;
            $banner->image2x = config('app.url').$banner->image2x;
            $banner->image3x = config('app.url').$banner->image3x;
        }

        return $this->success($banners);
    }

	/**
	 * 创建轮播图资源
	 *
	 * Date: 2018/2/28
	 * @author George
	 * @param Request $request
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \Throwable
	 */
    public function store(Request $request)
    {
        $requests = $this->xiuGaiFile($request, 1);

        $banner = new Banner();

        $attributes = $this->validate($requests, [
            'image' => 'required',
            'image2x' => 'required',
            'image3x' => 'required',
            'url' => 'nullable',
            'number' => 'nullable',
        ],[
            'image.required' => '图片地址不能为空',
            'image2x.required' => '2倍图图片地址不能为空',
            'image3x.required' => '3倍图图片地址不能为空',
        ]);

        foreach ($attributes as $k => $v){
            $banner->{$k} = $v;
        }

        $banner->saveOrFail();

        return $this->stored($banner);
    }

	/**
	 * 根据ID获取轮播图资源
	 *
	 * Date: 2018/2/28
	 * @author George
	 * @param Banner $banner
	 * @return \Illuminate\Http\JsonResponse
	 */
    public function show(Banner $banner)
    {
        return $this->success($banner);
    }

	/**
	 * 更新轮播图信息
	 *
	 * Date: 2018/2/28
	 * @author George
	 * @param UpdateBannerResource $request
	 * @param Banner $banner
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \Throwable
	 */
    public function update(UpdateBannerResource $request, Banner $banner)
    {
        $requests = $this->xiuGaiFile($request);

        $attributes = $this->validate($requests, [
            'image' => 'required',
            'image2x' => 'required',
            'image3x' => 'required',
            'url' => 'nullable',
            'number' => 'nullable',
        ],[
            'image.required' => '图片地址不能为空',
            'image2x.required' => '2倍图图片地址不能为空',
            'image3x.required' => '3倍图图片地址不能为空',
        ]);

        foreach ($attributes as $k => $v){
            if ($v) {
                $banner->{$k} = $v;
            }
        }

        $banner->saveOrFail();

        return $this->stored($banner);
    }

	/**
	 * 修改文件名称
	 *
	 * Date: 2018/2/28
	 * @author George
	 * @param $request
	 * @param int $is_tian
	 * @return mixed
	 */
    public function xiuGaiFile($request, $is_tian = 0){

        $num = substr_count($request->image,'/');
        $num2x = substr_count($request->image2x,'/');
        $num3x = substr_count($request->image3x,'/');

        $request['image'] = str_replace(config('app.url'), '', $request['image']);
        $request['image2x'] = str_replace(config('app.url'), '', $request['image2x']);
        $request['image3x'] = str_replace(config('app.url'), '', $request['image3x']);

        $name = time().rand(1, 100000);

        // 处理原图
        if ( $num >= 4 || $is_tian == 1) {
            $filePath = str_replace('/storage', '', $request->image);
            Storage::disk('public')->move($filePath, $name.'.png');
            $request['image'] = '/storage/'.$name.'.png';
        }

        // 处理2倍图
        if ( $num2x >= 4 || $is_tian == 1) {
            $filePath2x = str_replace('/storage', '', $request->image2x);
            Storage::disk('public')->move($filePath2x, $name.'@2x.png');
            $request['image2x'] = '/storage/'.$name.'@2x.png';
        }

        // 处理3倍图
        if ( $num3x >= 4 || $is_tian == 1) {
            $filePath3x = str_replace('/storage', '', $request->image3x);
            Storage::disk('public')->move($filePath3x, $name.'@3x.png');
            $request['image3x'] = '/storage/'.$name.'@3x.png';
        }

        return $request;

    }

	/**
	 * 删除轮播图资源
	 *
	 * Date: 2018/2/28
	 * @author George
	 * @param DestroyBannerResource $request
	 * @param Banner $banner
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \Exception
	 */
    public function destroy(DestroyBannerResource $request, Banner $banner)
    {
        $banner->delete();

        // 删除图片
        Storage::disk('public')->delete(str_replace('/storage', '', $banner->image));
        Storage::disk('public')->delete(str_replace('/storage', '', $banner->image2x));
        Storage::disk('public')->delete(str_replace('/storage', '', $banner->image3x));

        return $this->deleted();
    }


}
